<template>
  <div class="dashboard-container">
    <el-button type="normal" size="small" @click="dialogVisible = true">新增学科</el-button>

    <el-dialog title="学科添加" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="学科名称:" prop="subjectName">
          <el-input v-model="form.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示:">
          <el-switch v-model="form.isFrontDisplay"></el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="submitForm()">Confirm</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {add} from '@/api/hmmm/subjects'
export default {
  name: 'SubjectsAdd',
  data() {
    return {
      dialogVisible: false,
      form: {
        subjectName: '',
        isFrontDisplay: false
      },
      rules: {
        subjectName: [
          {
            required: true,
            message: 'Please input Activity name',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.submit()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    submit() {
      this.form.isFrontDisplay = this.form.isFrontDisplay ? 1 : 0
      add(this.form).then(data => {
        if (data.status === 200) {
          this.$emit('addSuccess', data.status)
          this.$message.success('添加成功')
        }
      }).catch(err => {
        his.$message.error('添加失败:' + err)
      })
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>
</style>
